package com.kaola.util;

import com.kaola.pojo.Admin;
import com.kaola.pojo.Category;
import com.kaola.pojo.Product;
import com.kaola.pojo.User;
import com.kaola.pojo.UserProduct;

public class FieldUtils {

	public static void generateDefaultField(Object bean) {
		if (bean.getClass() == User.class) {
			((User) bean).setCreateAt(System.currentTimeMillis());
			((User) bean).setUpdateAt(System.currentTimeMillis());
			((User) bean).setVersion(1);
		} else if (bean.getClass() == Category.class) {
			((Category) bean).setCreateAt(System.currentTimeMillis());
			((Category) bean).setUpdateAt(System.currentTimeMillis());
			((Category) bean).setVersion(1);
		} else if (bean.getClass() == Product.class) {
			((Product) bean).setCreateAt(System.currentTimeMillis());
			((Product) bean).setUpdateAt(System.currentTimeMillis());
			((Product) bean).setVersion(1);
		} else if (bean.getClass() == UserProduct.class) {
			((UserProduct) bean).setCreateAt(System.currentTimeMillis());
			((UserProduct) bean).setUpdateAt(System.currentTimeMillis());
			((UserProduct) bean).setVersion(1);
		} else if (bean.getClass() == Admin.class) {
			((Admin) bean).setCreateAt(System.currentTimeMillis());
			((Admin) bean).setUpdateAt(System.currentTimeMillis());
			((Admin) bean).setVersion(1);
		}
	}

	public static void modifyDefaultField(Object bean) {
		if (bean.getClass() == User.class) {
			long oldVersion = ((User) bean).getVersion();
			((User) bean).setUpdateAt(System.currentTimeMillis());
			((User) bean).setVersion(++oldVersion);
		} else if (bean.getClass() == Category.class) {
			long oldVersion = ((Category) bean).getVersion();
			((Category) bean).setUpdateAt(System.currentTimeMillis());
			((Category) bean).setVersion(++oldVersion);
		} else if (bean.getClass() == Product.class) {
			long oldVersion = ((Product) bean).getVersion();
			((Product) bean).setUpdateAt(System.currentTimeMillis());
			((Product) bean).setVersion(++oldVersion);
		} else if (bean.getClass() == UserProduct.class) {
			long oldVersion = ((UserProduct) bean).getVersion();
			((UserProduct) bean).setUpdateAt(System.currentTimeMillis());
			((UserProduct) bean).setVersion(++oldVersion);
		} else if (bean.getClass() == Admin.class) {
			long oldVersion = ((Admin) bean).getVersion();
			((Admin) bean).setUpdateAt(System.currentTimeMillis());
			((Admin) bean).setVersion(++oldVersion);
		}
	}
}
